$(function(){	
	init();
	bindFun();
	cancer();
});
//全局变量 保单号
var policyRef;
var contractId;
var result;
function init(){
	/*contractId = getQueryString("contractId");
	policyRef  = getQueryString("policyRef");*/
	contractId=sessionStorage.contractId;
	policyRef=sessionStorage.policyRef;
	$("#policyRef").html(policyRef);
	initPic();
	//长按图片删除事件
	doubleDelete();
}

function initPic(){
	var typeList=[];
	typeList.push("1152");
	typeList.push("1160");
	typeList.push("1163");
	typeList.push("1159");
	typeList.push("1153");
	typeList.push("1154");
	typeList.push("1171");
	padMarketing.ajaxNormal(
			"/pad_marketing/note/getPic.action",
			{	'typeList' : typeList,
				'policyRef':policyRef,
				'contractId':contractId},
			function(data){
					//初始化NEWTOUCH数据
					padMarketing.ajaxNormal(
							"/pad_marketing/note/initMyContent.action",
							{"contractId" :contractId},
							function(data){
								result = data.result;
								if(result.decla=="Y"){
									$("#decla").attr("class","check_yes");
									$("#declaraction").val(result.declation);
									$("#declaraction").removeAttr("disabled");
								}
							}
					);
					if(data.C){
						$("#addData").attr("class","check_yes");
					}
					
						if(data.C&&data.C.length!=0){
							//循环html
							var CL = data.C;
							for (var i in CL )
							{
								var flag =getNum(CL[i],"C") ;
								addPic("C",flag);
								$("#"+("imgC")+flag).attr("src","/pad_marketing/note/showPic.action?type="+("C_")+flag+"&policyRef="+policyRef+"&contractId="+contractId);
							}
						}else{
							addPic("C");
						}
						if(data.M&&data.M.length!=0){
							var ML = data.M;
							for (var j in ML)
							{
								var flag1=getNum(ML[j],"M")
								addPic("M",flag1);
								$("#"+("imgM")+flag1).attr("src","/pad_marketing/note/showPic.action?type="+("M_")+flag1+"&policyRef="+policyRef+"&contractId="+contractId);
							}
						}else{
							addPic("M");
						}
						if(data.O&&data.O.length!=0){
							var OL = data.O;
							for (var k in OL )
							{
								var flag2= getNum(OL[k],"O");
								addPic("O",flag2);
								$("#"+("imgO")+flag2).attr("src","/pad_marketing/note/showPic.action?type="+("O_")+flag2+"&policyRef="+policyRef+"&contractId="+contractId);
							}
						}else{
							addPic("O");
						}
						if(data.D&&data.D.length!=0){
							var DL = data.D;
							for (var k in DL )
							{
								var flag2= getNum(DL[k],"D");
								addPic("D",flag2);
								$("#"+("imgD")+flag2).attr("src","/pad_marketing/note/showPic.action?type="+("D_")+flag2+"&policyRef="+policyRef+"&contractId="+contractId);
							}
						}else{
							addPic("D");
						}
						if(data.S&&data.S.length!=0){
							var SL = data.S;
							for (var k in SL )
							{
								var flag2= getNum(SL[k],"S");
								addPic("S",flag2);
								$("#"+("imgS")+flag2).attr("src","/pad_marketing/note/showPic.action?type="+("S_")+flag2+"&policyRef="+policyRef+"&contractId="+contractId);
							}
						}else{
							addPic("F");
						}
						if(data.H&&data.H.length!=0){
							var HL = data.H;
							for (var k in HL )
							{
								var flag2= getNum(HL[k],"H");
								addPic("H",flag2);
								$("#"+("imgH")+flag2).attr("src","/pad_marketing/note/showPic.action?type="+("H_")+flag2+"&policyRef="+policyRef+"&contractId="+contractId);
							}
						}else{
							addPic("H");
						}
						if(data.L&&data.L.length!=0){
							var LL = data.L;
							for (var k in LL )
							{
								var flag2= getNum(LL[k],"L");
								addPic("L",flag2);
								$("#"+("imgL")+flag2).attr("src","/pad_marketing/note/showPic.action?type="+("L_")+flag2+"&policyRef="+policyRef+"&contractId="+contractId);
							}
						}else{
							addPic("L");
						}
						checkHtmlNull();
					}
	);
}



function bindFun(){
	$("#next").click(function(){
		//记录操作
		var cla = $("#decla").attr("class");
		var c =$("#addData").attr("class");
		if(cla=="check_yes"){
			result.decla="Y";
			result.declation=$("#declaraction").val();
			if(result.declation.length>=200){
				alertMsg("请您填写的声明控制在200字以内");
				return;
			}
			if(result.declation.length==0){
				alertMsg("请您将填写声明");
				return;
			}
			result.contractId=contractId;
		}else if(c!="check_yes"){
			alertMsg("请您勾选至少一中操作");
			return;
		}
		var typeList=[];
		typeList.push("1152");
		typeList.push("1160");
		typeList.push("1163");
		typeList.push("1159");
		typeList.push("1153");
		typeList.push("1154");
		typeList.push("1171");
		padMarketing.ajaxNormal(
				"/pad_marketing/note/getPic.action",
				{	'typeList' : typeList,
					'policyRef':policyRef,
					'contractId':contractId},
				function(data){
						var cla = $("#decla").attr("class");
						var flag;
						if(cla=="check_yes"){
							flag="Y";
							sessionStorage.declaraction = $("#declaraction").val();
						}else{
							flag="N";
							if(!data.C){
								alertMsg("至少完成一项操作");
								return;
							}
							
						}
						padMarketing.ajaxNormal(
								"/pad_marketing/note/updateMyContent.action",
								result,
								function(data){
									
								}
						);
						if(data.C){
							location.href="affirmNoteReply.action?C="+data.C.length+"&D="+data.D.length+"&H="+data.H.length+"&L="+data.L.length+"&M="+data.M.length+"&O="+data.O.length+"&S="+data.S.length+"&decla="+flag+"&contractId="+contractId+"&policyRef="+policyRef+"&url=noteIssue.action";
							return;
						}
						location.href="affirmNoteReply.action?decla="+flag+"&contractId="+contractId+"&policyRef="+policyRef+"&url=noteIssue.action";
						return;
					}
		);
	});
}
var photoNo=null;
var type=null;
//图片点击,调用壳的方法
function takePic(tpe,no){
	photoNo=no;
	type=tpe;
	closeDiv('open0');
	$("#MyPhoto").attr("onclick","goTakePhoto('"+type+"')");
	$("#MyAlbum").attr("onclick","openLibrary('"+type+"')");
	showDiv('open0');
}
//壳返回调用的方法 证件类
function C(i) {
	var base64 = 'data:image/png;base64,' + i;
	var idType="C_"+photoNo;
	closeDiv('open0');
	uploadOne(base64, idType,policyRef);
}
//壳返回调用的方法 财务类
function M(i) {
	var base64 = 'data:image/png;base64,' + i;
	var idType="M_"+photoNo;
	closeDiv('open0');
	uploadOne(base64, idType,policyRef);
}
//壳返回调用的方法 其他类
function O(i) {
	var base64 = 'data:image/png;base64,' + i;
	var idType="O_"+photoNo;
	uploadOne(base64, idType,policyRef);
}
//壳返回调用的方法 其他类
function D(i) {
	var base64 = 'data:image/png;base64,' + i;
	var idType="D_"+photoNo;
	closeDiv('open0');
	uploadOne(base64, idType,policyRef);
}
//壳返回调用的方法 其他类
function S(i) {
	var base64 = 'data:image/png;base64,' + i;
	var idType="S_"+photoNo;
	closeDiv('open0');
	uploadOne(base64, idType,policyRef);
}
//壳返回调用的方法 其他类
function H(i) {
	var base64 = 'data:image/png;base64,' + i;
	var idType="H_"+photoNo;
	closeDiv('open0');
	uploadOne(base64, idType,policyRef);
}
//壳返回调用的方法 其他类
function L(i) {
	var base64 = 'data:image/png;base64,' + i;
	var idType="L_"+photoNo;
	closeDiv('open0');
	uploadOne(base64, idType,policyRef);
}
//图片单独上传
function uploadOne(imageData, idType,policyRef) {
	padMarketing.ajaxNormal(
			"/pad_marketing/note/uploadOne.action",
			{	'image' : imageData,
				'idType' : idType,
				'policyRef':policyRef,
				'contractId':contractId},
			function(data){
					$("#img"+type+photoNo).attr('src',imageData);
				}
	);
}
//长按图片删除事件
function doubleDelete(){
	var t = null;
	$("input[class='input']").live('touchstart',function(target){
		t = setTimeout(function(){
			alertMsg("确定删除该照片？",1,function(){
				var src = $(target.target).next().attr("src");
				if(src==""){
					$(target.target).parent().remove();
					checkHtmlNull();
				}else{
					var id = $(target.target).attr("id");
					id=id.substring(0,1)+'_'+id.substring(1,id.length);
					padMarketing.ajaxNormal(
							"/pad_marketing/note/deletePic.action",
							{	'id' : id,
								'policyRef':policyRef,
								'contractId':contractId},
							function(data){
									$(target.target).parent().remove();
									checkHtmlNull();
								}
					);
				}
				

				
			});
		},1000);
	});
	$("input[class='input']").live('touchend',function(){
		clearTimeout(t);
	});
}

//点击增加上传框
var c      =1;
var o      =1;
var m      =1;
var d      =1;
var s      =1;
var h      =1;
var l      =1;
function addPic(type,num){
	var html="";
	c =$("input[name='C']").length;
	m = $("input[name='M']").length;
	o = $("input[name='O']").length;
	d = $("input[name='D']").length;
	s = $("input[name='S']").length;
	h = $("input[name='H']").length;
	l = $("input[name='L']").length;
	if(type=="C"){
		if(num){
			c=num;
		}else{
			var max = getMaxNum(type)
			c++;
			if(c==max){
				c++;
			}
		}
		html+='<div class="upload-img">';
		html+='<input type="file" id='+("C"+c)+' name="C" class="input" onclick="takePic('+"'C'"+','+c+')">';
		html+='<img src="" id='+("imgC"+c)+'>';
		html+='<h5>';
		html+='<p class="gray font-16 lh-30">请原件拍照或上传</p>';
		html+=' </h5>';
		html+='</div>';
		$("#CAll").append(html);
	}else if(type=="M"){
		if(num){
			m=num;
		}else{
			var max = getMaxNum(type)
			m++;
			if(m==max){
				m++;
			}
		}
		html+='<div class="upload-img">';
		html+='<input type="file" id='+("M"+m)+' name="M" class="input" onclick="takePic('+"'M'"+','+m+')">';
		html+='<img src="" id='+("imgM"+m)+'>';
		html+='<h5>';
		html+='<p class="gray font-16 lh-30">请原件拍照或上传</p>';
		html+=' </h5>';
		html+='</div>';
		$("#MAll").append(html);
	}else if(type=="O"){
		if(num){
			o=num;
		}else{
			var max = getMaxNum(type)
			o++;
			if(o==max){
				o++;
			}
		}
		html+='<div class="upload-img">';
		html+='<input type="file" id='+("O"+o)+' name="O" class="input" onclick="takePic('+"'O'"+','+o+')">';
		html+='<img src="" id='+("imgO"+o)+'>';
		html+='<h5>';
		html+='<p class="gray font-16 lh-30">请原件拍照或上传</p>';
		html+=' </h5>';
		html+='</div>';
		$("#OAll").append(html);
	}else if(type=="D"){
		if(num){
			d=num;
		}else{
			var max = getMaxNum(type)
			d++;
			if(d==max){
				d++;
			}
		}
		html+='<div class="upload-img">';
		html+='<input type="file" id='+("D"+d)+' name="D" class="input" onclick="takePic('+"'D'"+','+d+')">';
		html+='<img src="" id='+("imgD"+d)+'>';
		html+='<h5>';
		html+='<p class="gray font-16 lh-30">请原件拍照或上传</p>';
		html+=' </h5>';
		html+='</div>';
		$("#DAll").append(html);
	}else if(type=="S"){
		if(num){
			s=num;
		}else{
			var max = getMaxNum(type)
			s++;
			if(s==max){
				s++;
			}
		}
		html+='<div class="upload-img">';
		html+='<input type="file" id='+("S"+s)+' name="S" class="input" onclick="takePic('+"'S'"+','+s+')">';
		html+='<img src="" id='+("imgS"+s)+'>';
		html+='<h5>';
		html+='<p class="gray font-16 lh-30">请原件拍照或上传</p>';
		html+=' </h5>';
		html+='</div>';
		$("#SAll").append(html);
	}else if(type=="H"){
		if(num){
			h=num;
		}else{
			var max = getMaxNum(type)
			h++;
			if(h==max){
				h++;
			}
		}
		html+='<div class="upload-img">';
		html+='<input type="file" id='+("H"+h)+' name="H" class="input" onclick="takePic('+"'H'"+','+h+')">';
		html+='<img src="" id='+("imgH"+h)+'>';
		html+='<h5>';
		html+='<p class="gray font-16 lh-30">请原件拍照或上传</p>';
		html+=' </h5>';
		html+='</div>';
		$("#HAll").append(html);
	}else if(type=="L"){
		if(num){
			l=num;
		}else{
			var max = getMaxNum(type)
			l++;
			if(l==max){
				l++;
			}
		}
		html+='<div class="upload-img">';
		html+='<input type="file" id='+("L"+l)+' name="L" class="input" onclick="takePic('+"'L'"+','+l+')">';
		html+='<img src="" id='+("imgL"+l)+'>';
		html+='<h5>';
		html+='<p class="gray font-16 lh-30">请原件拍照或上传</p>';
		html+=' </h5>';
		html+='</div>';
		$("#LAll").append(html);
	}
	cancer();
	if(myScroll)
		myScroll.refresh(); 
}
function cancer(){
	$("input[type=file]").click(function(event) {
        event.preventDefault();
      });
}
//获得界面上 同一类型最大的图片
function getMaxNum(type){
	var max=0;
	$("input[name=\'"+type+"\']").each(function(index,me){
		 var id = parseInt(($(me).attr("id")).replace(type,""));
		 if(max<id){
			 max=id;
		 }
	});
	return max;
} 
//获得地址中的参数
function getNum(name,type){
	
	var arr = name.split("_")[2];
	return parseInt(arr);
}
// 刷新界面防止界面出现没有拍照框的情况
function checkHtmlNull(){
	var html ="";
	html=$("#CAll").html();
	if("-1"==html.indexOf("class")){
		addPic("C");
	}
	
	html=$("#MAll").html();
	if("-1"==html.indexOf("class")){
		addPic("M");
	}
	
	html=$("#OAll").html();
	if("-1"==html.indexOf("class")){
		addPic("O");
	}
	
	html=$("#DAll").html();
	if("-1"==html.indexOf("class")){
		addPic("D");
	}
	
	html=$("#SAll").html();
	if("-1"==html.indexOf("class")){
		addPic("S");
	}
	
	html=$("#HAll").html();
	if("-1"==html.indexOf("class")){
		addPic("H");
	}
	
	html=$("#LAll").html();
	if("-1"==html.indexOf("class")){
		addPic("L");
	}
	
}

function checkDecla(){
	var cla = $("#decla").attr("class");
	if(cla=="check_yes"){
		$("#declaraction").attr("disabled","disabled");
		$("#declaraction").val("");
	}else{
		$("#declaraction").removeAttr("disabled");
	}
}

//返回
function returnUrl(){
	var url = getQueryString("url");
	if(url==null){
		location.href="/pad_marketing/index/index.action";
		return;
	}
	location.href=url;
}